package com.spring.serviceimp.friendlink;

import java.util.Map;

import com.common.Constant;
import com.common.PicUpload;
import com.hibernate.dao.friendlink.FriendLinkDao;
import com.hibernate.pojo.FriendLink;
import com.spring.service.friendlink.FriendLinkService;
import com.struts.form.FriendLinkForm;
import org.apache.commons.beanutils.BeanUtils;
import org.apache.struts.upload.FormFile;

public class FriendLinkServiceImp implements FriendLinkService{
	
	private FriendLinkDao friendLinkDao=null;
	
	
	/**
	 * save friend link
	 */
	public boolean saveFriendLink(FriendLinkForm friendlinkform)
	{
		FriendLink fl=new FriendLink();
		FormFile file = friendlinkform.getLink_pic();
		String newfilename=null;
		if(file.getFileSize()>0)
		{
			//return folder name
			String datefile=new PicUpload().createFolder(Constant.FRIENDLINK_UPLOAD_PIC);
			//database save path
			newfilename=Constant.FRIENDLINK_DATABASE_SAVEPATH+datefile+"/";
			//save pic,return pic name
			newfilename+=new PicUpload().picUpload(Constant.FRIENDLINK_UPLOAD_PIC+datefile, file);
		}
		try
		{
			BeanUtils.copyProperties(fl, friendlinkform);
		}
		catch(Exception e)
		{
			e.printStackTrace();
		}
		if(file.getFileSize()>0)
		{
			fl.setLink_pic(newfilename);
		}
		fl.setLink("http://"+fl.getLink());
		return friendLinkDao.saveFriendLink(fl);
		
	}

	
	/**
	 * query friend link list
	 * @return
	 */
	public Map findFriendLink(int page)
	{
		return friendLinkDao.findFriendLink(page);
	}
	
	/**
	 * delete friend link
	 * @return
	 */
	public boolean deleteFriendLink(int id)
	{
		return friendLinkDao.deleteFriendLink(id);
	}
	
	/**
	 * audit friend link
	 * @return
	 */
	public boolean updateFriendLinkAudit(FriendLinkForm friendlinkform)
	{
		return friendLinkDao.updateFriendLinkAudit(friendlinkform);
	}
	
	public boolean deleteBatchMessage(String ids)
	{
		return friendLinkDao.deleteBatchMessage(ids);
	}
	
	
	public FriendLinkDao getFriendLinkDao() {
		return friendLinkDao;
	}

	public void setFriendLinkDao(FriendLinkDao friendLinkDao) {
		this.friendLinkDao = friendLinkDao;
	}

}
